﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PointTracker
{
    public partial class professor : System.Web.UI.Page
    {
        public List<CheckBox> checkBoxes;
        public List<Class> classes;

        protected void Page_Load(object sender, EventArgs e)
        {
            string userEmail = "";

            //if (!IsPostBack)
            {
                if (Session["User"] == null)
                {
                    Response.Redirect("/");
                }
                else if (((User)Session["User"]).role == UserRole.Professor)
                {
                    userEmail = ((User)Session["User"]).email;
                }
            }

            GetClasses2(userEmail);
        }

        public void GetClasses2(string userEmail)
        {
            /*List<Class> */classes = PointTracker.Class.getClasses(userEmail);

            if (classes.Count() > 0)
            {
                /*
                string table = "";
                foreach (Class singleClass in classes)
                {
                    table += "<br /><a href='/viewEditPoints.aspx?class=" + singleClass.crn + "' " + ">" + singleClass.name + "</a>, term: " + singleClass.term + ", year: " + singleClass.year;
                }

                if (table.Equals(""))
                    table = "<br />No available classes";

                divClasses.InnerHtml = table;
                */

                checkBoxes = new List<CheckBox>();

                Table table = new Table();
                table.ID = "classTable";
                Page.Form.Controls.Add(table);

                TableRow headerRow = new TableRow();

                TableHeaderCell hCellName = new TableHeaderCell();
                hCellName.Text = "Class name";
                hCellName.Width = 100;
                hCellName.Style.Add("font-weight", "bold");
                TableHeaderCell hCellTerm = new TableHeaderCell();
                hCellTerm.Text = "Term";
                hCellTerm.Width = 100;
                hCellTerm.Style.Add("font-weight", "bold");
                TableHeaderCell hCellYear = new TableHeaderCell();
                hCellYear.Text = "Year";
                hCellYear.Width = 50;
                hCellYear.Style.Add("font-weight", "bold");
                TableHeaderCell hCellDelete = new TableHeaderCell();
                hCellDelete.Text = "Delete class?";
                hCellDelete.Width = 100;
                hCellDelete.Style.Add("font-weight", "bold");

                headerRow.Cells.Add(hCellName);
                headerRow.Cells.Add(hCellTerm);
                headerRow.Cells.Add(hCellYear);
                headerRow.Cells.Add(hCellDelete);

                table.Controls.Add(headerRow);

                foreach (Class singleClass in classes)
                {
                    TableRow tempRow = new TableRow();

                    CheckBox cBox = new CheckBox();
                    cBox.Enabled = true;
                    checkBoxes.Add(cBox);

                    TableCell tempCellName = new TableCell();
                    tempCellName.Text = "<a href='/viewEditPoints.aspx?class=" + singleClass.crn + "' >" + singleClass.name + "</a>";
                    TableCell tempCellTerm = new TableCell();
                    tempCellTerm.Text = singleClass.term.ToString();
                    TableCell tempCellYear = new TableCell();
                    tempCellYear.Text = singleClass.year.ToString();
                    TableCell tempCellCbox = new TableCell();
                    tempCellCbox.Controls.Add(cBox);

                    tempRow.Cells.Add(tempCellName);
                    tempRow.Cells.Add(tempCellTerm);
                    tempRow.Cells.Add(tempCellYear);
                    tempRow.Cells.Add(tempCellCbox);

                    table.Controls.Add(tempRow);
                }

                divClasses.Controls.Add(table);
            }
            else
                ButtonDelete.Visible = false;
        }

        public void DeleteClasses(Object sender, EventArgs e)
        {
            Boolean successful = true;
            for (int x = 0; x < checkBoxes.Count(); x++)
            {
                if (checkBoxes[x].Checked)
                {
                    successful = successful && PointTracker.DatabaseManager.removeClass(classes[x].crn, classes[x].term, classes[x].year);
                }
            }

            if (successful)
            {
                Response.Redirect("~/Professor.aspx");
            }
            else
            {
                Response.Redirect("~/Error/Default.aspx");
            }
        }
    }
}